/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.carbswang.ohos.numberpickerview;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.text.Font;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;
import ohos.agp.window.service.WindowManager;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import cn.carbswang.ohos.numberpickerview.library.NumberPickerView;

/**
 * MainAbility
 *
 * @since 2021-08-11
 */
public class MainAbility extends Ability implements Component.ClickedListener,
    NumberPickerView.OnScrollListener, NumberPickerView.OnValueChangeListener,
    NumberPickerView.OnValueChangeListenerInScrolling {
    private static final HiLogLabel TAG = new HiLogLabel(HiLog.LOG_APP, 0xD001100, "NumberPickerView");

    private Button mButton1;
    private NumberPickerView mNumberPickerView;
    private ToastDialog toastDialog;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        WindowManager.getInstance().getTopWindow().get()
            .setStatusBarColor(getColor(ResourceTable.Color_colorPrimaryDark));
        getWindow().setStatusBarVisibility(Component.HIDE);
        setUIContent(ResourceTable.Layout_ability_main);

        mNumberPickerView = (NumberPickerView) findComponentById(ResourceTable.Id_main_picker);
        mButton1 = (Button) findComponentById(ResourceTable.Id_button1);
        mButton1.setClickedListener(this);
        findComponentById(ResourceTable.Id_button2).setClickedListener(this);
        findComponentById(ResourceTable.Id_button3).setClickedListener(this);
        findComponentById(ResourceTable.Id_button4).setClickedListener(this);
        findComponentById(ResourceTable.Id_button5).setClickedListener(this);
        findComponentById(ResourceTable.Id_button6).setClickedListener(this);
        findComponentById(ResourceTable.Id_button7).setClickedListener(this);
        findComponentById(ResourceTable.Id_button8).setClickedListener(this);

        mNumberPickerView.setOnScrollListener(this);
        mNumberPickerView.setOnValueChangedListener(this);
        mNumberPickerView.setOnValueChangeListenerInScrolling(this);

        String[] displayValues = getStringArray(ResourceTable.Strarray_test_display_2);
        mNumberPickerView.refreshByNewDisplayedValues(displayValues);
        getWrapState();
    }

    /**
     * 获取当前picker是否wrap
     */
    private void getWrapState() {
        if (mNumberPickerView.getWrapSelectorWheelAbsolutely()) {
            mButton1.setText(ResourceTable.String_switch_wrap_mode_true);
        } else {
            mButton1.setText(ResourceTable.String_switch_wrap_mode_false);
        }
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_button1:
                mNumberPickerView.setWrapSelectorWheel(!mNumberPickerView.getWrapSelectorWheelAbsolutely());
                getWrapState();
                break;
            case ResourceTable.Id_button2:
                String[] display_1 = getStringArray(ResourceTable.Strarray_test_display_1);
                mNumberPickerView.refreshByNewDisplayedValues(display_1);
                getWrapState();
                break;
            case ResourceTable.Id_button3:
                String[] display_2 = getStringArray(ResourceTable.Strarray_test_display_2);
                mNumberPickerView.refreshByNewDisplayedValues(display_2);
                getWrapState();
                break;
            case ResourceTable.Id_button4:
                int value = mNumberPickerView.getValue();
                mNumberPickerView.smoothScrollToValue(value, value + 2);
                break;
            case ResourceTable.Id_button5:
                getCurrentContent();
                break;
            case ResourceTable.Id_button6:
                Intent intent = new Intent();
                Operation operation = new Intent.OperationBuilder()
                    .withAbilityName(TimePickerAbility.class)
                    .withBundleName(getBundleName())
                    .build();
                intent.setOperation(operation);
                startAbility(intent);
                break;
            case ResourceTable.Id_button7:
                showNPVDialog();
                break;
            case ResourceTable.Id_button8:
                changeFont();
                break;
            default:
                break;
        }
    }

    private void showNPVDialog() {
        DialogNPV dialogNPV = new DialogNPV(this);
        dialogNPV.show();
    }

    private void getCurrentContent() {
        String[] content = mNumberPickerView.getDisplayedValues();
        if (content != null) {
            toast(getString(ResourceTable.String_picked_content_is)
                + content[mNumberPickerView.getValue() - mNumberPickerView.getMinValue()], "");
        }
    }

    private void changeFont() {
        Font font = FontUtil.parseAttributes(getContext());
        mNumberPickerView.setContentTextTypeface(font);
        mNumberPickerView.setHintTextTypeface(font);
        mNumberPickerView.postLayout();
    }

    @Override
    public void onScrollStateChange(NumberPickerView view, int scrollState) {
        HiLog.debug(TAG, "onScrollStateChange : " + scrollState);
    }

    @Override
    public void onValueChange(NumberPickerView picker, int oldVal, int newVal) {
        String[] content = picker.getDisplayedValues();
        if (content != null) {
            toast("oldVal : " + oldVal + " newVal : " + newVal,
                getString(ResourceTable.String_picked_content_is) + content[newVal - picker.getMinValue()]);
        }
    }

    @Override
    public void onValueChangeInScrolling(NumberPickerView picker, int oldVal, int newVal) {
        HiLog.debug(TAG, "onValueChangeInScrolling oldVal : " + oldVal + " newVal : " + newVal);
    }

    private void toast(String value, String content) {
        if (toastDialog != null) {
            toastDialog.remove();
            toastDialog.destroy();
            toastDialog = null;
        }
        DirectionalLayout toastLayout = (DirectionalLayout) LayoutScatter.getInstance(getContext())
            .parse(ResourceTable.Layout_picker_toast_layout, null, false);
        Text tvValue = (Text) toastLayout.findComponentById(ResourceTable.Id_tv_value);
        Text tvContent = (Text) toastLayout.findComponentById(ResourceTable.Id_tv_content);
        tvValue.setText(value);
        if (content.isEmpty()) {
            tvContent.setVisibility(Component.HIDE);
        } else {
            tvContent.setText(content);
        }

        toastDialog = new ToastDialog(getContext());
        toastDialog.setContentCustomComponent(toastLayout);
        toastDialog.setSize(DirectionalLayout.LayoutConfig.MATCH_CONTENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT);
        toastDialog.setAlignment(LayoutAlignment.BOTTOM);
        toastDialog.setTransparent(true);
        toastDialog.show();
    }
}